package com.didispace.chapter36.service;

import com.didispace.chapter36.entity.User;
import com.didispace.chapter36.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.Executor;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private Executor taskExecutor1;
    @Autowired
    private AsyncService asyncService;
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Transactional(rollbackFor = Exception.class, isolation = Isolation.REPEATABLE_READ)
    public void save1() {
        User user1 = new User();
        user1.setName("李春天");
        user1.setAge(22);
        insert(user1);

        User user2 = new User();
        user2.setName("张冬天");
        user2.setAge(122);
        insert(user2);

        eventPublisher.publishEvent(new UserRegisterEvent(user1));
        throw new RuntimeException("测试上面");
    }



    public void insert(User user) {
        userMapper.insert(user);
    }

}
